package com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.api.OpenApiClient;
import com.actionsoft.bpms.api.common.ApiResponse;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.sdk.service.model.TaskQueryModel;
import com.actionsoft.sdk.service.response.BoolResponse;
import com.actionsoft.sdk.service.response.MapResponse;
import com.actionsoft.sdk.service.response.StringResponse;
import com.actionsoft.sdk.service.response.process.ProcessInstResponse;
import com.actionsoft.sdk.service.response.task.TaskInstsGetResponse;
import com.alibaba.fastjson.JSONObject;
/*
 * 检验项目预约提交功能
 * phonenum 身份证号
 * reviewdate预约复查日期
 * totalprice项目总价
 * ListStr 检查项目明细列表
 */

@Controller
public class VerifyReservationSubmissionCmd {
	
	@Mapping("com.awspaass.user.apps.VerifyReservationSubmissionOrder")
	public String submitVerifyReservationOrder(UserContext uc,String phonenum, String reviewdate,String totalprice, String ListStr) {
		
		List<Itemslist> ItemList = JSONObject.parseArray(ListStr, Itemslist.class);
		System.out.println("接收的结果是：" + ListStr);
		OpenApiClient client = new OpenApiClient("https://www.sht808.com/portal/openapi", "opapiuser4326",
				"Abcd123456@#");
		JSONObject result = new JSONObject();
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("processDefId", "obj_13ffe9697e4543678f7470c275faecf4");
		args.put("uid", uc.getUID());
		args.put("title", uc.getUserName() + "发起的预约复查");

		ProcessInstResponse r = client.exec("process.create", args, ProcessInstResponse.class);
		com.actionsoft.sdk.service.model.ProcessInstance processInst = r.getData();

		System.out.println("已经创建了一个流程 ：" + r.getData().getId());
		String bindid = processInst.getId();

		Map<String, Object> start = new HashMap<String, Object>();
		start.put("processInstId", processInst.getId());
		ApiResponse rstart = client.exec("process.start", start, ApiResponse.class);

		Map<String, Object> taskQueryArgs = new HashMap<String, Object>();
		TaskQueryModel taskQueryModel = new TaskQueryModel();
		
		taskQueryModel.setTarget(uc.getUID());// 这里要换成用户的uid
		taskQueryModel.setProcessInstId(bindid);
		taskQueryArgs.put("tqm", taskQueryModel);
		TaskInstsGetResponse taskQueryResult = client.exec("task.query", taskQueryArgs, TaskInstsGetResponse.class);

		if (!taskQueryResult.getResult().equals("ok")) {
			result.put("status", "1");
			result.put("message", "任务启动失败：" + taskQueryResult.getMsg());
			return result.toString();
		} else {
			com.actionsoft.sdk.service.model.TaskInstance taskInstance = taskQueryResult.getData().get(0);
			String taskInstanceId = taskInstance.getId();
			Map<String, Object> boFoodOrder = new HashMap<String, Object>();
			boFoodOrder.put("boName", "BO_EU_RESERVATIONORDER");//预约订单主表
			Map<String, Object> mapFoodOrder = new HashMap<String, Object>();
			mapFoodOrder.put("UNIT", uc.getCompanyModel().getExt2());//预定人单位
			mapFoodOrder.put("WORKERID", uc.getUserModel().getUID());//预定人uid
			mapFoodOrder.put("PEOPLENAME", uc.getUserModel().getUserName());//预定人名称
			mapFoodOrder.put("PHONENUM", uc.getUserModel().getMobile());//预定人电话
			mapFoodOrder.put("IDNUM", phonenum);//身份证号
			mapFoodOrder.put("REVIEWDATE", reviewdate);//预约复查日期
			mapFoodOrder.put("FEEAMOUNT", totalprice);//项目总价

			boFoodOrder.put("recordData", mapFoodOrder);
			boFoodOrder.put("bindId", bindid);
			boFoodOrder.put("uid", uc.getUID());
			StringResponse rbo = client.exec("bo.create", boFoodOrder, StringResponse.class);
			System.out.println("初始化BO：" + rbo.getData());
			String BoId = rbo.getData();

			// Create TableBody BO_EU_SHSYST_FOODORDER_BODY
			for (int i = 0; i < ItemList.size(); i++) {
				Map<String, Object> boFoodBody = new HashMap<String, Object>();
				boFoodBody.put("boName", "BO_EU_RESERVATIONORDER_B");//预约订单子表

				Map<String, Object> mapFoodBody = new HashMap<String, Object>();
				Itemslist item = ItemList.get(i);
				mapFoodBody.put("PROJECTNO", item.getProjectno());//项目编号
				mapFoodBody.put("ITEMNAME", item.getItemname());//项目名称
				mapFoodBody.put("PRICE", item.getPrice());//单价
				mapFoodBody.put("ITEMID", item.getItemid());//项目ID
				mapFoodBody.put("SFCOST", "是");//是否自费
				
				boFoodBody.put("recordData", mapFoodBody);
				boFoodBody.put("bindId", bindid);
				boFoodBody.put("uid", uc.getUID());// 系统登录账号

				StringResponse rmxbo = client.exec("bo.create", boFoodBody, StringResponse.class);
				System.out.println("初始化BO：" + rmxbo.getData());
				String MxBoId = rmxbo.getData();

			}

			Map<String, Object> taskCommentCommitArgs = new HashMap<String, Object>();
			taskCommentCommitArgs.put("taskInstId", taskInstanceId);
			taskCommentCommitArgs.put("user", uc.getUID());

			taskCommentCommitArgs.put("actionName", "提交");
			taskCommentCommitArgs.put("commentMsg", "");
			taskCommentCommitArgs.put("isIgnoreDefaultSetting", false);
			BoolResponse taskCommentCommitResult = client.exec("task.comment.commit", taskCommentCommitArgs,
					BoolResponse.class);
			if (!taskCommentCommitResult.getResult().equals("ok")) {
				result.put("status", "1");
				result.put("message", "error commit" + taskCommentCommitResult.getMsg());
			} else {

				Map<String, Object> taskCompleteArgs = new HashMap<String, Object>();
				taskCompleteArgs.put("taskInstId", taskInstanceId);
				taskCompleteArgs.put("uid", uc.getUID());
				MapResponse taskCompleteResult = client.exec("task.complete", taskCompleteArgs, MapResponse.class);
				if (!taskCompleteResult.getResult().equals("ok")) {
					result.put("status", "1");
					result.put("message", "error commit" + taskCompleteResult.getMsg());

				} else {
					result.put("status", "0");
					result.put("message", "提交成功");

				}
			}
		}

		return result.toString();
	}
	/*
	 * projectno 项目编号
     * itemname 项目名称
     * price 单价
     * itemid 项目ID
	 */
	public static class Itemslist {
		private String projectno;
		private String itemname;
		private float price;
		private String itemid;
		public String getProjectno() {
			return projectno;
		}
		public void setProjectno(String projectno) {
			this.projectno = projectno;
		}
		public String getItemname() {
			return itemname;
		}
		public void setItemname(String itemname) {
			this.itemname = itemname;
		}
		public float getPrice() {
			return price;
		}
		public void setPrice(float price) {
			this.price = price;
		}
		public String getItemid() {
			return itemid;
		}
		public void setItemid(String itemid) {
			this.itemid = itemid;
		}
		
	}
}
